/**
 * Copyright (c) Martijn Grootens (m.e.grootens@student.utwente.nl), 
 * University of Twente.
 * Created july 2012.
 *
 * This file is part of dbcleaner.
 * 
 * dbcleaner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * dbcleaner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with dbcleaner.  If not, see <http://www.gnu.org/licenses/>.
 */

package dbcleaner.core.parse;
import java.util.*;
import dbcleaner.core.tags.*;
import java.io.*;

/**
 * Utils for reading the lists of tags and correctly translating them
 * into TagLists.
 */
public class TagListFactory {
    
    // Create a TagList from file, without assigning the TagList a name.
    public static TagList read_TagList (File f) {
        return read_TagList(f,"no_name");
    }
    
    // Create a TagList from file, and assign the TagList a name.
    public static TagList read_TagList (File f, String s) {
        
        TagList tl = new TagList(s);
        
        try {
            
            // Scanner for the file
            Scanner scan = new Scanner(f);
            
            // A line and a line after splitting it about its commans.
            String ln = "";
            String[] split;
			String new_name;
            int num_pieces;
            
            while (scan.hasNextLine()) {
                
                // Fetch new line.
                ln = scan.nextLine();
                
                // Remove all spaces.
                ln = ln.replaceAll(" ", "");
                
                // The tags that were copied from the website may contain "_"'s.
                // The tags as found in the BibTeX file will have "\_" instead of
                // just the underscore; for comparison, replace "_" from the online
                // tags to "\_".
                ln = ln.replaceAll("_","\\\\_");
                
                // Split the tag about the arrow.
                split = ln.split("->",2);
                
                // Count the number of pieces
                num_pieces = split.length;
                
                if (num_pieces==1)
                    // The Tag should be printed as its original name.
                    tl.add_tag(new Tag(split[0]));
                else if (num_pieces==2) {
                    // The tag should be printed differently from its
                    // original name.
					new_name = split[1];
					new_name = new_name.replaceAll(",",", ");
                    tl.add_tag(new Tag(split[0],new_name));
				} else {
                    System.out.println("Error in tag splitting: " + ln);
                    System.exit(1);
                }
                
            }
            
        }
        catch (Exception e) {
            System.out.println(e);
            System.exit(1);
        } 
        
        
        return tl;
        
    }

}